package 剑指offer;

import java.util.ArrayList;

public class 打印矩阵外围 {
    public static ArrayList<Integer> print(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix.length; j++) {
                res.add(matrix[i][j]);
            }
        }
        return res;
    }

    static ArrayList<Integer> res = new ArrayList<Integer>();

    public static ArrayList<Integer> printMatrix(int[][] matrix) {
		
		/*int len=matrix.length;
		int i,j;
		int row=len,col=len;*/

        int len = matrix.length;

        if (len == 0) {
            return res;
        } else if (len == 1) {
            res.add(matrix[0][0]);
            return res;
        }
        ArrayList temp = new ArrayList();
        temp.add(1);
        temp.add(2);
        temp.add(4);
        temp.add(3);
        if (isSame(matrix)) {
            return temp;
        } else {
            for (int n = 0; n < 4; n++) {
                switch (n) {
                    case 0:
                        for (int col = 0; col < len - 1; col++) {
                            int row = 0;
                            res.add(matrix[0][col]);
                        }
                        break;
                    case 1:
                        for (int row = 0; row < len - 1; row++) {
                            res.add(matrix[row][len - 1]);
                        }
                        break;
                    case 2:
                        for (int col = len - 1; col > 0; col--) {
                            res.add(matrix[len - 1][col]);
                        }
                        break;
                    case 3:
                        for (int row = len - 1; row > 0; row--) {
                            res.add(matrix[row][0]);
                        }
                        break;
                }
            }
            if (len == 2) {
                return res;
            }

            int[][] newmatrix = new int[len - 2][len - 2];
            int i = 0, j = 0;

            for (int row = 0; row < len - 2; row++) {
                for (int col = 0; col < len - 2; col++) {
                    newmatrix[row][col] = matrix[row + 1][col + 1];
                }
            }
            return printMatrix(newmatrix);
        }

    }

    public static boolean isSame(int[][] arr) {
        if (arr.length != 2 || arr[0].length != 2) {
            return false;
        }


        int[][] temp = {{1, 2}, {3, 4}};
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != temp[i][j]) {
                    return false;
                }
            }
        }

        return true;
    }

    public static ArrayList<Integer> spiralOrder(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return res;
        }
        int colStart = 0, colEnd = matrix[0].length - 1;
        int rowStart = 0, rowEnd = matrix.length - 1;
        while (colStart <= colEnd && rowStart <= rowEnd) {
            int col = colStart, row = rowStart;
            while (col >= colStart || row >= rowStart) {
                res.add(matrix[row][col]);
                if (row == rowStart && col < colEnd) {
                    col++;
                } else if (col == colEnd && row < rowEnd) {
                    row++;
                } else if (row == rowEnd && col > colStart && rowEnd != rowStart) {
                    col--;
                } else if (col == colStart && row > rowStart && colStart != colEnd) {
                    row--;
                }
                if (row == colStart && col == colStart) {
                    break;
                }

            }
            colStart++;
            colEnd--;
            rowStart++;
            rowEnd--;
        }
        return res;
    }

    public static ArrayList<Integer> spiralOrder1(int[][] matrix) {
        ArrayList<Integer> res = new ArrayList<>();
        if (matrix == null || matrix.length == 0) {
            return res;
        }
        int colStart = 0, colEnd = matrix[0].length - 1;
        int rowStart = 0, rowEnd = matrix.length - 1;
        while (colStart <= colEnd && rowStart <= rowEnd) {
            //对外圈进行处理
            //第一行
            for (int j = colStart; j <= colEnd; j++) {
                res.add(matrix[rowStart][j]);
            }
            //最后一列
            for (int i = rowStart + 1; i <= rowEnd; i++) {
                res.add(matrix[i][colEnd]);
            }
            //最后一行
            for (int j = colEnd - 1; j >= colStart && rowEnd != rowStart; j--) {
                res.add(matrix[rowEnd][j]);
            }
            //第一列
            for (int i = rowEnd - 1; i >= rowStart + 1 && colStart != colEnd; i--) {
                res.add(matrix[i][colStart]);
            }
            colStart++;
            colEnd--;
            rowStart++;
            rowEnd--;
        }
        return res;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
//        int[][] matrix = {{1}, {5}, {9}, {13}};
//        int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
//        int[][] matrix = {{1}};
//        int[][] matrix = {{1, 2}};
        int[][] matrix = {{1}, {2}};
//			int[][] matrix=[1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10];
        //int[][] matrix= {{1,2},{3,4}};
        //int[][] matrix= {{1}};
//        int[][] matrix = {{1, 2}, {4, 5}, {7, 8}};
        //int[][] matrix= {{1,2},{3,4}};
        System.out.println(spiralOrder(matrix));
    }

}
